/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"F1FBB74D-6899-4320-90FD-EFCD49EDF631"}
 */
var fPass = null;

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"B0DA57DE-901F-4C08-BFA7-AB1768207B4A"}
 */
function onShow(firstShow, event) {
	controller.loadRecords(globals.gEmpresa)
	databaseManager.setAutoSave(false)
	elements.logo.imageURL = icono_url
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"96F25F1C-8A72-4366-A0CD-C0D4AB3597DF"}
 */
function guardar(event) {
	
	if (forms.empresa_dtl_pass.fPassAnterior && forms.empresa_dtl_pass.fPassNuevo2 && forms.empresa_dtl_pass.fPassNuevo)
	{
		if (utils.validatePBKDF2Hash(forms.empresa_dtl_pass.fPassAnterior, hash_pass))
		{
			if (forms.empresa_dtl_pass.fPassNuevo == forms.empresa_dtl_pass.fPassNuevo2)
			{
				hash_pass = utils.stringPBKDF2Hash(forms.empresa_dtl_pass.fPassNuevo,2000)
			}
			else
			{
				globals.DIALOGS.showWarningDialog('Aviso','Las contraseñas nuevas no coinciden','OK')
			}
		}
		else
		{
			globals.DIALOGS.showWarningDialog('Aviso','Las contraseña actual no es correcta','OK')
		}
	}
	
	if (fPass)
		mail_pass = globals.cryptString(fPass, globals.cryptKey, 'C')
	
	if (databaseManager.saveData())
		globals.DIALOGS.showInfoDialog('Aviso','Los cambios fueron guardados','OK')
	else
		globals.DIALOGS.showWarningDialog('Aviso','Ocurrió un error al grabar los cambios','OK')
}

/**
 * Handle hide window.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"DFCF5E22-081B-4A70-B147-F243DE7610A2"}
 */
function onHide(event) {
	databaseManager.revertEditedRecords()
	databaseManager.setAutoSave(true)
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"77A9C80E-9980-4018-975C-ECECF8AFFA11"}
 */
function buscarLogo(event) {
	plugins.file.showFileOpenDialog(null, null, false, new Array("Images", "jpg", "gif", "png", "*"), grabarLogo, 'Seleccione el logo')
}

/**
 * @param {Array<plugins.file.JSFile>} foto
 *
 * @properties={typeid:24,uuid:"9390AF36-674C-4E9E-B3E7-0EE588E85DED"}
 */
function grabarLogo(foto)
{
	var $arch 	= foto[0]
	var $nombre = $arch.getName()
	
	if ($arch.getContentType().indexOf('image') == -1)
	{
		globals.DIALOGS.showWarningDialog('Aviso','El archivo debe ser una imagen','OK')
		return
	}
	
	var $img 	= plugins.images.getImage($arch.getBytes())
	
	var $dir 	= plugins.file.getDefaultUploadLocation()+ '/fac_electronica/'+ empresa_id
	var $folder = plugins.file.createFile($dir)
	var f 		= plugins.file.convertToJSFile($folder);
	if (!f.exists())
		plugins.file.createFolder($dir)

	var $file = plugins.file.createFile($dir+'/'+$nombre)
	
	var $data = $img.getData()//$arch.getBytes();
	if (plugins.file.writeFile($file, $data, $arch.getContentType()))
	{
		icono_url = application.getServerURL()+'/uploads/fac_electronica/'+ empresa_id+ '/' + $nombre
		elements.logo.imageURL = icono_url
	}
	else
		globals.DIALOGS.showWarningDialog('Aviso','No se pudo subir la imagen','OK')
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"58909D88-7AF9-433F-BA5B-BBD678CF5B10"}
 */
function altaContacto(event) {
	empresas_to_empresa_contactos.newRecord()
}
